package com.meterhuang.thread.foobar;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author meterhuang
 * @date 2021/09/08 15:54
 */
@Slf4j
public class FooBarPrint {

	public static void main(String[] args) {
		//FooBarBlockingQueue fooBarBlockingQueue = new FooBarBlockingQueue(4);
		//FooBarReentrant fooBarReentrant = new FooBarReentrant(4);
		//FooBarSynchronized fooBarSynchronized = new FooBarSynchronized(5);
		FooBarSemaphore fooBarSemaphore = new FooBarSemaphore(100);
		Runnable r1 = new PrintFooThread();
		Runnable r2 = new PrintBarThread();
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
				2,
				2,
				10000,
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(3),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.DiscardPolicy()
		);

		threadPool.submit(() -> {
			try {
				fooBarSemaphore.foo(r1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

		threadPool.submit(() -> {
			try {
				fooBarSemaphore.bar(r2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
	}

}
